def prcalc():
    import sys
    import time
    import readinp
    import readmodel
    import applymodel
    import setprob
    import mcrun
    import CheckParam
    # Read input and output file name
    if __name__ == '__main__':

        try:
            ifn=sys.argv[1]
        except:
            print("Specify input file name.")
            sys.exit(2)

        try:
            ofn=sys.argv[2]
        except:
            print("Specify output file name.")
            sys.exit(2)

    else:
        ifn=input(' -Specify input file name:')
        ofn=input(' -Specify output file name:')

    # Temperary log file
    t0=time.time()
    lfn="log%.0f" % t0

    # Open files
    ifp=open(ifn.strip(),'r')
    ofp=open(ofn.strip(),'w+r')
    lfp=open(lfn.strip(),'w')


    # Check file name
    fstr=" Input file: "+ifn+" "+"Output file: "+ofn+" "+"Log file: "+lfn
    print("\n"+fstr)
    lfp.write("\n"+fstr+"\n"*2)

    # Main
    readinp.readinp(ifp)
    CheckParam.check_readinp(lfp)

    readmodel.readmodel()
    CheckParam.check_model(lfp)

    applymodel.ApplyModel(lfp)
    setprob.SetProb()
    CheckParam.check_path(lfp)

    mcrun.MCRun(ofp)

    # Calculate running time
    t1=time.time()
    str_dt=" Total running time = %.0f (sec)\n" % (t1-t0)
    print(str_dt)
    lfp.write(str_dt)

    # Close file
    ifp.close()
    lfp.close()
    ofp.close()

# Execute main function
prcalc()
